// pages/order/orderComment.js
import config from '../../config.js';
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tag: [{
      text: "服务态度好",
      value: "0",
      checked: false,
    }, {
      text: "服务质量高",
      value: "1",
      checked: false,
    }, {
      text: "服务及时",
      value: "2",
      checked: false,
    }, {
      text: "专业性高",
      value: "3",
      checked: false,
    }],

    exp: [{
      text: "车内整洁干净",
      value: "0",
      checked: false,
    }, {
      text: "收费合理",
      value: "1",
      checked: false,
    }],
    //评价
    commentOrderId: 0,
    commentContent: '',
    commentScore: 0,
    commentMark: '',
  },


  tagSelect: function(e) {
    var tagArr = this.data.tag;
    var checkArr = e.detail.value;
    for (var i = 0; i < tagArr.length; i++) {
      if (checkArr.indexOf(i + "") != -1) {
        tagArr[i].checked = true;
      } else {
        tagArr[i].checked = false;
      }
    }
    this.setData({
      tag: tagArr
    })
  },

  expSelect: function(e) {
    var tagArr = this.data.exp;
    var checkArr = e.detail.value;
    for (var i = 0; i < tagArr.length; i++) {
      if (checkArr.indexOf(i + "") != -1) {
        tagArr[i].checked = true;
      } else {
        tagArr[i].checked = false;
      }
    }
    this.setData({
      exp: tagArr
    })
  },

  inputSug: function(e) {
    var sug = e.detail.value;
    if (sug) {
      this.setData({
        commentContent: sug
      })
    }
  },


  star: function(e) {
    let score = e.currentTarget.dataset.score;
    console.log(typeof score);
    this.setData({
      commentScore: score,
    })
  },

  //提交评价
  submitComment: function() {
    var tag = this.data.tag,
      exp = this.data.exp,
      that = this,
      commentMark = this.data.commentMark;
    for (var i = 0; i < tag.length; i++) {
      if (tag[i].checked) {
        commentMark = commentMark + tag[i].text + ",";
      }
    }
    for (var i = 0; i < exp.length; i++) {
      if (exp[i].checked) {
        commentMark = commentMark + tag[i].text + ",";
      }
    }
    this.setData({
      commentMark: commentMark
    })
    if (!this.data.commentScore) {
      wx.showToast({
        title: '请打分',
        icon: 'none'
      })
      return;
    }
    if (!this.data.commentMark) {
      wx.showToast({
        title: '请选择服务体验',
        icon: 'none'
      })
      return;
    }
    //提交评价
    wx.request({
      url: base.orderComment,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: that.data.commentOrderId,
        content: that.data.commentContent,
        mark: that.data.commentMark,
        score: that.data.commentScore,
      },
      method: "POST",
      success: function(res) {
        if (res.data.status == 200) {
          wx.showToast({
            title: res.data.message,
          })
        }

        wx.navigateBack({
          
        })
      },
      complete: function() {
        that.setData({
          commentMark: ''
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var orderId = options.orderid;
    if (orderId) {
      this.setData({
        commentOrderId: orderId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})